#include "ImageProxy.h"
#include <thread>
#include <future>

ImageProxy::ImageProxy(const std::string& placeholderImage)
	:_image(std::make_shared<Image>(placeholderImage)),
	_mutex()
{	
}

std::shared_ptr<IImage> ImageProxy::New(const std::string& realImage, const std::string& placeholderImage)
{
	auto instance = std::shared_ptr<ImageProxy>(new ImageProxy(placeholderImage));
	auto _ = std::async(std::launch::async, [instance, realImage]() { instance->Load(realImage); });
	return instance;
}

void ImageProxy::Draw()
{
	std::lock_guard lock(_mutex);
	_image->Draw();
}

void ImageProxy::Load(const std::string& realImage)
{
	//模拟耗时加载过程
	std::this_thread::sleep_for(std::chrono::milliseconds(300));

	std::lock_guard lock(_mutex);
	_image = std::make_shared<Image>(realImage);
}
